package cn.com.surker.workcon.common;

import cn.com.surker.base.system.entity.SysStorageLog;
import cn.com.surker.redis.RedisUtils;
import cn.com.surker.spring.SpringContextHolder;
import cn.com.surker.util.MathUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用于模拟消耗的工具类
 *
 * @author luotao
 * @date 2025/3/4
 */
public class OptimizeUtil {

	private static final BigDecimal baseValue = new BigDecimal(100);
	//默认偏差率
	private static final BigDecimal defaultDeviation = new BigDecimal(1.3);

	private static final String OPTIMIZE_MATERIAL_STORAGE_KEY = "OPTIMIZE_MATERIAL_STORAGE_KEY";

	// private static RedisUtils redisUtils = SpringContextHolder.getBean("redisUtils");
	private static RedisUtils redisUtils = null;

	/**
	 * 获取仓位
	 *
	 * @param materialType  材料类型
	 * @param wcStorageList 工控机仓位列表
	 */
	public static SysStorageLog simulateStorage(Long lineId, Long materialType, List<SysStorageLog> wcStorageList) {
		if (lineId == null || materialType == null || wcStorageList == null || wcStorageList.isEmpty()) {
			return null;
		}
		SysStorageLog sysStorageLog = null;
		//	拼接key
		String key = OPTIMIZE_MATERIAL_STORAGE_KEY + "_" + lineId + "_" + materialType;
		//	取出之前的仓位
		Object o = redisUtils.get(key);
		if (o != null) {
			sysStorageLog = (SysStorageLog) o;
			return sysStorageLog;
		}
		//	之前没有数据或者数据为null,设置新的数据,从列表中随机一个
		int size = wcStorageList.size();
		Random random = new Random();
		int randomNumber = random.nextInt(size);
		sysStorageLog = wcStorageList.get(randomNumber);
		//	存入redis
		redisUtils.set(key, sysStorageLog);
		return sysStorageLog;
	}


	/**
	 * 创建随机消耗量
	 *
	 * @param batchVolume      盘方量
	 * @param theoreticalValue 设定值
	 * @param randRate         上下浮动率
	 * @param accurate         精确位数
	 * @return
	 */
	public static Double simulateUsed(Double batchVolume, Double theoreticalValue, Double randRate, Integer accurate) {
		//方量
		BigDecimal bvb = new BigDecimal(batchVolume);
		//配比
		BigDecimal mvb = new BigDecimal(theoreticalValue);
		//根据材料配比*方量，得出理论值
		BigDecimal setValue = bvb.multiply(mvb);
		//	偏差率
		BigDecimal deviation = defaultDeviation;
		if (randRate != null) {
			deviation = BigDecimal.valueOf(randRate);
		}
		//	精确位数
		if (accurate == null) {
			accurate = 0;
		}
		//最小偏差
		BigDecimal minDifference = baseValue.subtract(deviation).divide(baseValue);
		//最大偏差
		BigDecimal maxDifference = baseValue.add(deviation).divide(baseValue);
		//根据理论值计算最小值
		Double minValue = setValue.multiply(minDifference).doubleValue();
		//根据理论值计算最大值
		Double maxValue = setValue.multiply(maxDifference).doubleValue();
		//随机出真实消耗值
		Double resRealValue = getRandomValue(maxValue, minValue, accurate);
		return resRealValue;
	}

	/**
	 * 获取随机数
	 *
	 * @param minValue 最小值
	 * @param maxValue 最大值
	 * @param accurate 精确位数
	 * @return
	 */
	private static Double getRandomValue(Double minValue, Double maxValue, Integer accurate) {
		Random random = new Random();
		//取出最大最小值的差值
		double difference = maxValue - minValue;
		//最小值加随机数
		double tvalue = random.nextDouble() * difference + minValue;
		BigDecimal bigDecima = new BigDecimal(tvalue);
		double finalValue = bigDecima.setScale(accurate, BigDecimal.ROUND_HALF_UP).doubleValue();
		return finalValue;
	}

}
